'use strict';

angular.module('app')
	.controller('accountRecController', [ '$rootScope', '$scope', '$http', '$state',
	                                function($rootScope, $scope, $http, $state) {
		$scope.title = '应收账款';
        $scope.param = { };
        $scope.loading = false;
		$scope.search = function () {
	        $scope.loading = true;
            $scope.param.applyMinDate =$('#applyMinDate').val();
            $scope.param.applyMaxDate =$('#applyMaxDate').val();
            $scope.param.approveMinDate =$('#approveMinDate').val();
            $scope.param.approveMaxDate =$('#approveMaxDate').val();
			$.ajax({
				url : $scope.app.project_name+'/accountRec/read/list',
				data: $scope.param
			}).then(function(result) {
		        $scope.loading = false;
				if (result.httpCode == 200) {
					$scope.pageInfo = result.data;
				} else {
					$scope.msg = result.msg;
				}
				$scope.$apply();
			});
		}
        $scope.searchRe = function () {
           $scope.loading = true;
           $scope.param.pageNum = 1;
            $scope.param.applyMinDate =$('#applyMinDate').val();
            $scope.param.applyMaxDate =$('#applyMaxDate').val();
            $scope.param.approveMinDate =$('#approveMinDate').val();
            $scope.param.approveMaxDate =$('#approveMaxDate').val();
           $.ajax({
                 url : $scope.app.project_name+'/accountRec/read/list',
                 data: $scope.param
           }).then(function(result) {
              $scope.loading = false;
              if (result.httpCode == 200) {
                    $scope.pageInfo = result.data;
               } else {
                    $scope.msg = result.msg;
                 }
                       $scope.$apply();
		   });
      }

          $scope.searchRe();


        $rootScope.selected = [];
		var updateSelected = function (action, id,iden,state) {
		if (action == 'add' && $rootScope.selected.indexOf(id) == -1&&iden=='2'&&state=='1') {
            $rootScope.selected.push(id);
		}
		if (action == 'remove' && $rootScope.selected.indexOf(id) != -1&&iden=='2'&&state=='1') {
            $rootScope.selected.splice($scope.selected.indexOf(id), 1);
		}
		};
		// 全选操作
		$scope.selectAll = function ($event) {
		var checkbox = $event.target;
		var action = (checkbox.checked ? 'add' : 'remove');
		for (var i = 0; i < $scope.pageInfo.records.length; i++) {
		var contact = $scope.pageInfo.records[i];
		updateSelected(action, contact.id,contact.iden,contact.rechargeStatus);
		 }
		 var check=document.getElementsByName("selected");
            for (var i = 0; i < check.length; i++) {
                check[i].checked=document.getElementById("all").checked;
            }
		};

		$scope.updateSelection=function ($event, id,iden,state) {
		var checkbox = $event.target;
		var action = (checkbox.checked ? 'add' : 'remove');
		updateSelected(action, id,iden,state);
		}

		$scope.isSelected = function (id) {
		return  $rootScope.selected.indexOf(id) >= 0;
		};
		//批量通过/失败
		$scope.batchApprove=function (state) {
          var remark=$('#remark').val();
          if(state==2){
            $scope.alertMessage({
                text: "确定做此操作吗？", //内容
                btnCancel: true, //取消按钮
                btnConfirm: true //确认按钮
            }).then(function () {
                $.ajax({
                    url : $scope.app.project_name + '/accountRec/update/batchApprove',
                    data: {'ids':$rootScope.selected, 'state':state,remark:remark}
                }).then(function(result) {
                    $scope.loading = false;
                    if (result.httpCode == 200) {
                        $scope.alertModal({"content":"操作成功"});
                        $rootScope.selected=[];
                        $scope.firstApply=false
                        $scope.search();
                    } else {
                        $scope.msg = result.msg;
                    }
                    $scope.$apply();
                });
            });
        }else{
              $.ajax({
                  url : $scope.app.project_name + '/accountRec/update/batchApprove',
                  data: {'ids':$rootScope.selected, 'state':state,remark:remark}
              }).then(function(result) {
                  $scope.loading = false;
                  if (result.httpCode == 200) {
                      $scope.alertModal({"content":"操作成功"});
                      $rootScope.selected=[];
                      $scope.firstApply=false
                      $('#remark').val("");
                      $scope.search();
                  } else {
                      $scope.msg = result.msg;
                  }
                  $('#remark').val("");
                  $scope.$apply();
              });

		  }
			}


		$scope.disableItem = function(id, enable) {
			$.ajax({
				url : $scope.app.project_name + '/accountRec/update/disable',
				data: {'id': id, 'enable' : enable}
			}).then(function(result) {
		        $scope.loading = false;
				if (result.httpCode == 200) {
					$scope.param.keyword= null;
					$scope.search();
				} else {
					$scope.msg = result.msg;
				}
			});
		}
		
		// 翻页
        $scope.pagination = function (page) {
            $scope.param.pageNum=page;
            $scope.search();
        };

		$scope.preview = function(){
			$scope.firstApply = true;
		}
        $scope.clearSearch = function() {
            $scope.param= {};
            $('#applyMinDate').val("");
          $('#applyMaxDate').val("");
            $('#approveMinDate').val("");
           $('#approveMaxDate').val("");
            $scope.search();
        }


} ]);